package com.toolbox.service.impl;

import com.toolbox.service.PPTService;
import org.apache.poi.xslf.usermodel.*;
import org.springframework.stereotype.Service;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

@Service
public class PPTServiceImpl implements PPTService {

    private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");

    @Override
    public InputStream generatePPT(String title, String templateType, List<Map<String, Object>> slides) throws IOException {
        // 创建一个新的PPT文档
        XMLSlideShow ppt = new XMLSlideShow();
        
        // 设置页面大小
        ppt.setPageSize(new java.awt.Dimension(960, 540));
        
        // 创建标题幻灯片
        XSLFSlide titleSlide = ppt.createSlide();
        XSLFTextBox titleBox = titleSlide.createTextBox();
        titleBox.setAnchor(new java.awt.Rectangle(50, 100, 860, 200));
        XSLFTextParagraph titleParagraph = titleBox.addNewTextParagraph();
        XSLFTextRun titleRun = titleParagraph.addNewTextRun();
        titleRun.setText(title);
        titleRun.setFontSize(48.0);
        titleRun.setBold(true);
        
        // 添加副标题
        XSLFTextBox subtitleBox = titleSlide.createTextBox();
        subtitleBox.setAnchor(new java.awt.Rectangle(50, 300, 860, 100));
        XSLFTextParagraph subtitleParagraph = subtitleBox.addNewTextParagraph();
        XSLFTextRun subtitleRun = subtitleParagraph.addNewTextRun();
        subtitleRun.setText("生成时间: " + new java.util.Date());
        subtitleRun.setFontSize(24.0);
        
        // 根据幻灯片内容创建正文幻灯片
        for (Map<String, Object> slide : slides) {
            XSLFSlide contentSlide = ppt.createSlide();
            
            // 添加幻灯片标题
            if (slide.containsKey("title")) {
                XSLFTextBox slideTitleBox = contentSlide.createTextBox();
                slideTitleBox.setAnchor(new java.awt.Rectangle(50, 50, 860, 80));
                XSLFTextParagraph slideTitleParagraph = slideTitleBox.addNewTextParagraph();
                XSLFTextRun slideTitleRun = slideTitleParagraph.addNewTextRun();
                slideTitleRun.setText(slide.get("title").toString());
                slideTitleRun.setFontSize(36.0);
                slideTitleRun.setBold(true);
            }
            
            // 添加幻灯片内容
            if (slide.containsKey("content")) {
                XSLFTextBox contentBox = contentSlide.createTextBox();
                contentBox.setAnchor(new java.awt.Rectangle(50, 150, 860, 300));
                XSLFTextParagraph contentParagraph = contentBox.addNewTextParagraph();
                XSLFTextRun contentRun = contentParagraph.addNewTextRun();
                contentRun.setText(slide.get("content").toString());
                contentRun.setFontSize(20.0);
            }
        }
        
        // 保存PPT到临时文件
        String filename = "generated_ppt_" + System.currentTimeMillis() + ".pptx";
        Path tempFilePath = Paths.get(TEMP_DIR, filename);
        
        try (FileOutputStream out = new FileOutputStream(tempFilePath.toFile())) {
            ppt.write(out);
        }
        
        // 返回文件输入流
        return new FileInputStream(tempFilePath.toFile());
    }
}